package com.cl.short_video_java.web.admin;


import com.cl.short_video_java.annotation.IsRole;
import com.cl.short_video_java.annotation.Log;
import com.cl.short_video_java.annotation.Login;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.mapper.VideoUserMapper;
import com.cl.short_video_java.menum.BusinessType;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.vo.user.AddUserAdmin;
import com.cl.short_video_java.vo.user.UserSearch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/user")
public class UserController {

	@Resource
	private VideoUserService videoUserService;

	@Resource
	private VideoUserMapper videoUserMapper;

	/**
	 * 新镇用户
	 *
	 * @param videoUser
	 * @return
	 */
	@PostMapping("/insert")

	public Result InsertVideoUser(@RequestBody VideoUser videoUser) {
		return Result.success(videoUserMapper.insert(videoUser));
	}

	/**
	 * 通过id修改用户其余信息
	 *
	 * @param videoUser
	 * @return
	 */
	@PostMapping("/updata")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 23)
	@Log(title = "用户信息",businessType = BusinessType.UPDATE)
	public Result UpdataVideoUserById(@RequestBody VideoUser videoUser) {
		return Result.success(videoUserMapper.updateById(videoUser));
	}


	/**
	 * 用户删除
	 *
	 * @param idlist 参数：1，2，3
	 * @return
	 */
	@PostMapping("/delete")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 26)
	@Log(title = "用户信息",businessType = BusinessType.DELETE)
	public Result DeleeteVideoUserById(@RequestBody List<Integer> idlist) {
		return Result.success(videoUserService.DeleteVideoUserByIdService(idlist));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @return
	 */
	@PostMapping("/page"
	)@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	public Result selectAllVideoUser(@RequestBody UserSearch userSearch) {
		return Result.success(videoUserService.SelectAllVideoUserService(userSearch));
	}

	@PostMapping("/vip")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 11)
	public Result vipUserList(@RequestBody UserSearch userSearch) {
		return Result.success(videoUserService.vipUserList(userSearch));
	}

	/**
	 * 用户角色等级分配
	 *
	 * @param id    用户id
	 * @param level 用户角色：0普通，1会员
	 * @return
	 */
	@PostMapping("/updatalevel")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	@Log(title = "用户角色修改",businessType = BusinessType.UPDATE)
	public Result UpdataVideoUserLevel(@RequestParam("id") Integer id, @RequestParam("level") Integer level) {
		return Result.success(videoUserService.UpdataVideoUserLevelService(id, level));
	}

	/**
	 * 用户是否管理员权限分配
	 *
	 * @param id      用户id
	 * @param isadmin 参数：0否，1是
	 * @return
	 */
	@PostMapping("/updataisadmin")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	@Log(title = "权限分配",businessType = BusinessType.UPDATE)
	public Result UpdataVideoUserIsAdmin(@RequestParam("id") Integer id, @RequestParam("isadmin") Integer isadmin) {
		return Result.success(videoUserService.UpdataVideoUserIsAdminService(id, isadmin));
	}

	/**
	 * 用户搜索
	 *
	 * @param userSearch 用户搜索对象
	 * @return
	 */
	@PostMapping("/search")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	public Result SearchVideoUser(@RequestBody UserSearch userSearch) {
		List<VideoUser> list = videoUserService.search(userSearch);
		return Result.success(list);
	}

	/**
	 * 新增获取修改用户
	 *
	 * @param user
	 * @return
	 */
	@PostMapping("/save")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	@Log(title = "用户新增/修改", businessType = BusinessType.INSERT)
	public Result saveUser(@Validated @RequestBody AddUserAdmin user) {
		videoUserService.save(user);
		return Result.success();
	}

	/**
	 * 禁用/解禁用户
	 *
	 * @param userId
	 * @return Result 封装结果集
	 */
	@GetMapping("/limit")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	@Log(title = "禁用/解禁用户", businessType = BusinessType.UPDATE)
	public Result limitUser(Integer userId) {
		videoUserService.limit(userId);
		return Result.success();
	}

	@PostMapping("/del")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	@Log(title = "删除用户", businessType = BusinessType.DELETE)
	public Result delUser(@RequestBody List<Integer> idList) {
		videoUserService.del(idList);
		return Result.success();
	}

	@PostMapping("/publishing")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 10)
	@Log(title = "发布功能",businessType = BusinessType.UPDATE)
	public Result publiShing(@RequestParam("id") Integer id,@RequestParam("isPubliShing") Integer isPubliShing){
		return Result.success(videoUserService.publiShingService(id,isPubliShing));
	}
}

